package cn.exrick.xboot.modules.base.serviceimpl.mybatis;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.modules.base.dao.mapper.UserMapper;
import cn.exrick.xboot.modules.base.entity.User;
import cn.exrick.xboot.modules.base.service.mybatis.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * User接口实现
 * @author portal
 */
@Slf4j
@Service
@Transactional
public class IUserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 分页获取user
     * @param page
     * @return
     */
    @Override
    public IPage<User> getUserByPage(Page page){
        return userMapper.getUserByPage(page);
    }

    @Override
    public List<User> getAdminUser(String orgId, int isAdmin) {
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(User::getDepartmentId,orgId);
        queryWrapper.eq(User::getIsAdmin,isAdmin);
       return  this.list(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void editAdminUser(String[] ids) {
        //1.先将全部用户设置成普通用户
        LambdaUpdateWrapper<User> updateWrapper=new LambdaUpdateWrapper();
        //普通用户
        updateWrapper.set(User::getType,CommonConstant.USER_TYPE_NORMAL);
        this.update(updateWrapper);
        //2.edit管理员名单
        LambdaUpdateWrapper<User> updateAdmin=new LambdaUpdateWrapper();
        //管理员用户
        updateAdmin.set(User::getType,CommonConstant.USER_TYPE_ADMIN);
        List<String>  list=new ArrayList();
        for (String id : ids) {
            list.add(id);
        }
        updateAdmin.in(User::getId,list);
        boolean update = this.update(updateAdmin);
    }
    /**
     * 根据机构id判断该机构下是否有员工用户
     *
     * @param orgId 机构id
     * @return 该机构下是否有员工，true是，false否
     * @author njp
     * @date 2020/8/11 10:01
     */
   /* @Override
   public boolean hasOrgUser(String orgId){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getOrgId, orgId);

        List<User> list = this.list(queryWrapper);
        return list.size() != 0;
    }*/

    /**
     * 修改员工相关机构信息
     *
     * @param orgId 机构id
     * @param orgName 机构名称
     * @author njp
     * @date 2020/8/11 15:47
     */
 /*   @Override
   public  void updateUserOrgInfo(String orgId, String orgName){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getOrgId, orgId);
        this.list(queryWrapper).forEach(user -> {
            user.setOrgName(orgName);
            this.updateById(user);
        });
    }*/

}